
var Pager = new Class({
    Implements: [Events, Options],
        options: {
        'tagid': null
    },
    initialize: function(options) {
        if(!options.tagid) return;
        this.setOptions(options);

        this.currentPage = 0;
    },
    setData: function(curPage, cntPage, onPage, rowsLength) {
        this.options.tagid.empty();
        if(!rowsLength) return;

        curPage = curPage || 0;
        this.pages = Math.floor((cntPage % onPage == 0) ? cntPage / onPage : cntPage / onPage + 1);

        if(this.pages != 1) {
            var page = curPage + 1, start = 1, end = this.pages, offSetEnd = false;

            // left arrow
            var tt = new Element('tt[style=color:teal;]', {
                'html': ' Page: ' + page + ' of ' + this.pages + ' (' + (curPage * onPage + 1) + '...' +
                        ((page != this.pages) ? (page * onPage) : (curPage * onPage + rowsLength)) + '/' +
                        cntPage + ')&nbsp;&nbsp;&nbsp;'
            });
            var elm = new Element('a[href=javascript:;][html=&nbsp;&laquo;&nbsp;]');
            if(page != 1) {
                elm.addEvents({
                    'click': this.onNewpage.bind(this, page - 2),
                    'focus': function() {this.blur();}
                });
            }
            this.options.tagid.adopt(tt, elm);

            if(page > 10) {
                start = page - 9;
                new Element('a', {
                    'href': 'javascript:;',
                    'text': '1 . . .',
                    'events':{
                        'click': this.onNewpage.bind(this, 0),
                        'focus': function() {this.blur();}
                    }
                }).inject(this.options.tagid);
            }

            if(this.pages - page > 9) {
                end = page + 9;
                offSetEnd = true;
            }
            for(var pg = start; pg <= end; pg++) {
                elm = new Element('a[href=javascript:;][text=' + pg + ']');
                if(pg == page) {
                    elm.setStyles({'color': '#800000', 'font-weight': 'bold'});
                }
                else {
                    elm.addEvent('click', this.onNewpage.bind(this, pg - 1));
                }
                this.options.tagid.adopt(elm);
            }

            if(offSetEnd) {
                new Element('a[href=javascript:;][text=. . . ' + this.pages + ']', {
                    'events':{
                        'click': this.onNewpage.bind(this, this.pages - 1),
                        'focus': function() {this.blur();}
                    }
                }).inject(this.options.tagid);
            }

            elm = new Element('a[href=javascript:;][html=&nbsp;&raquo;]');
            if(page != this.pages) {
                elm.addEvents({
                    'click': this.onNewpage.bind(this, page),
                    'focus': function() {this.blur();}
                });
            }
            this.options.tagid.adopt(elm);
        }
    },
    onNewpage: function(page) {
        this.currentPage = page;
        this.fireEvent('newpage', this.currentPage);
    }
});
